package en.edu.zxj.springrabbimqdemo.config;

import en.edu.zxj.springrabbimqdemo.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * Description：工作模式相关声明
 *
 * @author: zxj
 * @date: 2024-09-20
 * @time: 14:22:39
 */
@Configuration
public class RabbitMQFanoutConfig {
    // 1. 声明交换机
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE_NAME).durable(true).build();
    }
    // 2. 声明队列
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1() {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE_NAME1).build();
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2() {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE_NAME2).build();
    }
    // 3. 绑定队列到交换机
    @Bean("bindingFanoutQueue1ToExchange")
    public Binding bindingFanoutQueue1ToExchange(@Qualifier("fanoutQueue1") Queue fanoutQueue1, @Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }
    @Bean("bindingFanoutQueue2ToExchange")
    public Binding bindingFanoutQueue2ToExchange(@Qualifier("fanoutQueue2") Queue fanoutQueue2, @Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }
}
